#!/bin/bash
ulimit -c unlimited
if [[ ! -d src ]]
then
	echo "src directory does not exist"
	exit 1
fi
[[ -d out  ]] || mkdir out
buildWithStackProtector() {
	gcc -g3 -Wall -fPIC -c src/libverbose.c  -o out/libverbose.o && \
    gcc -g3 -Wall -fPIC -c src/config.c  -o out/config.o && \
	ld -ldl -lpthread -shared -soname libverbose.so -o out/libverbose.so -lc out/libverbose.o out/config.o && \
	LD_PRELOAD=$PWD/out/libverbose.so /bin/true 2>&1 | egrep -qv '__stack_chk_fail_local'
}
buildWithOutStackProtector() {
    gcc -g3 -Wall -fno-stack-protector -fPIC -c src/libverbose.c -o out/libverbose.o && \
    gcc -g3 -Wall -fPIC -c src/config.c  -o out/config.o && \
    ld -ldl -lpthread -shared -soname libverbose.so -o out/libverbose.so -lc out/libverbose.o out/config.o
}
if ! buildWithStackProtector && ! buildWithOutStackProtector
then
	echo "libverbose.so was not built"
	exit 2
fi
